package com.nsn.das.midas.report;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nsn.das.midas.model.mml.data.MMLCommandData;
import com.nsn.das.midas.model.mml.data.MMLGroupData;
import com.nsn.das.midas.model.mml.data.MasterListData;
import com.nsn.das.midas.service.MasterlistReport;

public class Forbidg1DotP38 extends GenerateFile implements MasterlistReport{	
	@Override
	public void generateFile(MasterListData masterListData) {		
		// TODO Auto-generated method stub
		createFile(masterListData);				
	}
	@Override
	protected String getOutPutFileName() {
		// TODO Auto-generated method stub
		return "forbidg1.p38";
	}

	@Override
	protected String getHeader() {
		// TODO Auto-generated method stub
		String time=getTime();
		listHead.add("$DEBUG COMPACT PAGEWIDTH(112) PAGELENGTH(42)");
		listHead.add("$WORD16");
		listHead.add("$TITLE('FORBIDG1')");		
		listHead.add("/*********************************************************************");
		listHead.add("* MODULE:  */ FORBIDG1 /*"+ Spc(45) + "*");
		listHead.add("* AUTHOR:  " +masterListData.getAuthor().getUsername()+Spc(58 - masterListData.getAuthor().getUsername().length())+"*");
		listHead.add("* MASTERLIST: " + masterListData.getName()  + " " + masterListData.getVersion().getVersion() + Spc(49 - masterListData.getName().length()) + "*");		
		listHead.add("* DATE: " + time + Spc(61 - time.length()) + "*");		
		listHead.add("* Copyright (c) " + time.substring(time.length()-4, time.length()) + " Nokia Corporation" + Spc(31) + "*");		
		listHead.add("*********************************************************************/");
		listHead.add(" : DO;");
		listHead.add("");
		listHead.add("$EJECT");		
		listHead.add("/*********************************************************************");
		listHead.add("*" + Spc(68)+ "*");    
		listHead.add("*                     INTERFACE DEFINITIONS" + Spc(26) + "*");
		listHead.add("*" + Spc(68) + "*");       
		listHead.add("*********************************************************************/");
		listHead.add("$INCLUDE(FORBIDG1.PIN)");
		listHead.add("");
		listHead.add("$EJECT");
		listHead.add("/*********************************************************************");
		listHead.add("*" + Spc(68)+ "*");       
		listHead.add("*                     MODULE IDENTIFICATION CODE" + Spc(21) + "*"); 
		listHead.add("*" + Spc(68)+ "*");       
		listHead.add("*********************************************************************/");
		if(masterListData.getState().getDescription()!="DRAFT"){
			listHead.add("/* %MID% */");
		}else{
			listHead.add("/* MASTERLIST: " + masterListData.getName() + " " + masterListData.getVersion().getVersion() + " " + 
					masterListData.getState().getDescription() + "*/");
		}
		listHead.add("");
		listHead.add("$EJECT");	
		listHead.add("/*********************************************************************");
		listHead.add("*" +Spc(68)+ "*");          
		listHead.add("*                     FILE CONTENTS " + Spc(33) + "*");
		listHead.add("*" + Spc(68)+ "*");          
		listHead.add("*********************************************************************/");
		listHead.add("DECLARE");
		listHead.add("     FORBIDG1_RECORD (*) FORBID_T DATA (");
		listHead.add("");    
		listHead.add(" /* command and MML -group that forbids this command */");
		listHead.add("");
		return turnListToString(listHead);
	}

	@Override
	protected String getBody() {
		// TODO Auto-generated method stub
		List<String> commonForbids = new ArrayList<String>();
		List<String> forbids=new ArrayList<String>();
		for(MMLGroupData gr:masterListData.getGroupes()){
			commonForbids=getCommonForbids(gr);
		}
		return turnListToString(listBody);
	}

	@Override
	protected String getTail() {
		// TODO Auto-generated method stub
		listTail.add("        0,0,0,0,0,");
		listTail.add("        'XXX','XX',");
		listTail.add("        'XXX','XX',");
		listTail.add("        0,0,0,0,0,");
		listTail.add("        'XXX','XX',");
		listTail.add("        'XXX','XX',");
		listTail.add("        0,0,0,0,0,");
		listTail.add("        'XXX','XX',");
		listTail.add("        'XXX','XX',");
		listTail.add("        0,0,0,0,0,");
		listTail.add("        'XXX','XX',");
		listTail.add("        'XXX','XX',");
		listTail.add("        0,0,0,0,0 );");
		listTail.add("END FORBIDG1;");
		return turnListToString(listTail);
	}
	private List<String> getCommonForbids(MMLGroupData groupData){
		Map<String,Integer> map=new HashMap<String,Integer>();
		List<String> forbids=new ArrayList<String>();
		for(MMLCommandData com:groupData.getCommandDatas()){
			forbids=getForbids(com);
			for(String forbid:forbids){
				if(map.containsKey(forbid)){
					map.put(forbid, map.get(forbid)+1);
				}else{
					map.put(forbid, 1);
				}
				
			}
		}
		
		return forbids;
	}
	private List<String> getForbids(MMLCommandData commandData){
		List<String> forbids=new ArrayList<String>();
		String str=commandData.getPreventing();
		String[] arr;
		arr=str.split(",");
		for(String item:arr){
			forbids.add(item);
		}
		return forbids;
	}
}